package com.topnav.mc.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.topnav.mc.admin.dao.mapper.OrganizationMapper;
import com.topnav.mc.admin.entity.Organization;
import com.topnav.mc.admin.entity.bean.SiteBean;
import com.topnav.mc.admin.entity.param.SiteParam;
import com.topnav.mc.base.mc.McServiceImpl;
import com.topnav.mc.document.entity.Document;
import com.topnav.mc.document.service.IDocumentService;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.SiteMapper;
import com.topnav.mc.admin.entity.Site;
import com.topnav.mc.admin.service.ISiteService;
import com.topnav.mc.base.CommServiceImpl;

@Service
public class SiteServiceImpl extends McServiceImpl<SiteMapper,Site, SiteBean, SiteParam> implements ISiteService{
	@Autowired
	private OrganizationMapper organizationMapper;
	@Autowired
	private IDocumentService documentService;

	@Override
	public boolean beforeSave(Site entity){
		if (this.getOne(new QueryWrapper<Site>().eq("code",entity.getCode()), true) != null){
			//throw new CustomException(-1,"站点  %s 已存在",entity.getCode());
			throw ExceptionUtil.commonException(ExceptionMsg.ERROR_PARAM_COLLECT_SITE_EXISTS);
		}
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		return true;
	}

	@Override
	public boolean beforeUpdate(Site entity){
		QueryWrapper<Site> wrapper = new QueryWrapper<Site>()
				.eq("code", entity.getCode()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			//throw new CustomException(-1,"站点  %s 已存在",entity.getCode());
			throw ExceptionUtil.commonException(ExceptionMsg.ERROR_PARAM_COLLECT_SITE_EXISTS);
		}
		entity.setUpdateTime(new Date());
		return true;

	}

	@Override
	public boolean beforeDelete(Site entity){
		//用户文件
		LambdaQueryWrapper<Document> documentWrapper = new LambdaQueryWrapper<>();
		documentWrapper.eq(Document::getSiteId,entity.getId());
		if (documentService.getOne(documentWrapper,false) != null){
			throw ExceptionUtil.commonException(ExceptionMsg.ERROR_DELETE_SITE_USED_BY_DOCUMENT);
		}
		return true;
	}

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<Site> list(Wrapper<Site> queryWrapper) {
		QueryWrapper<Site> wrapper = new QueryWrapper<Site>();
		if (queryWrapper.getEntity() == null){
			//return getInfo(getBaseMapper().selectList(queryWrapper));
			return getBaseMapper().selectList(queryWrapper);
		}
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}
			if (queryWrapper.getEntity().getType()!=null){
				wrapper.eq("type", queryWrapper.getEntity().getType());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getHost())){
				wrapper.eq("host", queryWrapper.getEntity().getHost());
			}
			if (queryWrapper.getEntity().getPort()!=null && queryWrapper.getEntity().getPort() != 0){
				wrapper.eq("port", queryWrapper.getEntity().getPort());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
				wrapper.eq("code", queryWrapper.getEntity().getCode());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
				wrapper.eq("name", queryWrapper.getEntity().getName());
			}
			if (queryWrapper.getEntity().getOrgId()!=null){
				wrapper.eq("org_id", queryWrapper.getEntity().getOrgId());
			}
		}
        //return getInfo(getBaseMapper().selectList(wrapper));
		return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<Site>> E page(E page, Wrapper<Site> queryWrapper) {
		QueryWrapper<Site> wrapper = new QueryWrapper<Site>();
		if (queryWrapper.getEntity() != null){
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}
			if (queryWrapper.getEntity().getType()!=null){
				wrapper.eq("type", queryWrapper.getEntity().getType());
			}

			if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
				wrapper.like("code", queryWrapper.getEntity().getCode());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
				wrapper.like("name", queryWrapper.getEntity().getName());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getHost())){
				wrapper.like("host", queryWrapper.getEntity().getHost());
			}
			if (queryWrapper.getEntity().getOrgId()!=null){
				wrapper.eq("org_id", queryWrapper.getEntity().getOrgId());
			}
		}
		E e = getBaseMapper().selectPage(page, wrapper);
		//e.setRecords(getInfo(e.getRecords()));
		return e;
    }

/*	private List<Site> getInfo(List<Site> sites){
		List<Long> ids = new ArrayList<Long>();
		*//** Organization *//*
		for(Site s : sites){
			if (s.getOrgId() != null && !ids.contains(s.getOrgId())){
				ids.add(s.getOrgId() );
			}
		}
		if (ids.size() > 0){
			List<Organization>  list = organizationMapper.selectBatchIds(ids);
			for (Organization o : list){
				for(Site origin : sites){
					if (o.getId().equals(origin.getOrgId())){
						origin.setOrgName(o.getName());
						origin.setOrganization(o);
					}
				}
			}
		}
		return sites;
	}*/
}
